#encoding=utf8

import random
import logging


def commonDice(hp, player):
	m = player.get('dec_enemy_dice')
	if m>=hp:
		player['dec_enemy_dice'] = m - hp
		return list()
	else:
		player['dec_enemy_dice'] = 0
		return [random.randint(1,6) for i in range(0,hp-m)]


def exileDiceFac(qs, txt):
	def exileDice(dices, player, enemy):
		cnt = 0
		for q in qs:
			cnt = cnt + dices.count(q)
		if cnt>0:	
			player['exile'] = player['exile'] + cnt	
			player['exile_txt'] = txt
	return exileDice		


def fixedDiceFac(cnt):
	def fixedDice(hp, player):
		return [random.randint(1,6) for i in range(0,cnt)]
	return fixedDice


def nextLevelFac(mid, levelcnt):
	def nextLevel(hp, player, enemy):
		if not player.has_key(mid):
			player[mid] = 0
		if player[mid]<levelcnt and not player.get('fast_next'):	
			player['fast_next'] = True
			player[mid] = player[mid] + 1
	return nextLevel	


def attackWeakFac(hits):
	def attackWeak(dices, player, enemy):
		cnt = 0;
		for h in hits:
			cnt = cnt + dices.count(h)
		player['weak'] = cnt	
		if cnt<=0:
			return
		player['enemy_hit'] = player['enemy_hit'] - cnt
		weak = None
		for mem in player.get('team'):
			if mem.get('st')>0:
				if not weak or weak.get('hp')>mem.get('hp'):
					weak = mem
		if weak:
			weak['hp'] = weak.get('hp') - cnt
			if weak.get('hp')<0:
				weak['hp'] = 0
				weak['st'] = 0
	return attackWeak	


def killAllFac(mid, cnt):
	def killAll(dices, player, enemy):
		if not player.has_key(mid):
			player[mid] = 0
		if player.get(mid)==cnt:	
			for mem in player.get('team'):
				mem['st'] = -1
		player[mid] = player[mid] + 1		
	return killAll		


def hurtBack(dices, player, enemy):
	hurt = enemy.get('round_hurt')
	player['enemy_hit'] = player.get('enemy_hit') + hurt
	enemy['round_hurt'] = 0


def queueDice(dices, player, enemy):
	cnt = 6 - len(dices)
	if enemy.get('round_hurt') > 6:
		cnt = cnt + enemy.get('round_hurt') - 6
	if cnt<=0:
		return
	newds = [random.randint(1,6) for i in range(0, cnt)]
	hit = newds.count(5) + newds.count(6)
	player['enemy_hit'] = player['enemy_hit'] + hit


def gotoDie(dices, player, enemy):
	enemy['hp'] = 0


def mustHurtFac(hit):
	def mustHurt(dices, player, enemy):
		player['enemy_hit'] = player.get('enemy_hit') + hit
	return mustHurt	


def iceFrezz(dices, player, enemy):
	for mem in player.get('team'):
		if mem.get('st')==-2:
			mem['st'] = 1
	frezz = random.randint(0,3)
	mem = player.get('team')[frezz]
	if mem.get('st')>0:
		mem['st'] = -2

		
def poison(dices, player, enemy):
	gotoDie(dices, player, enemy)
	player['poison'] = True


def runaway(dices, player, enemy):
	if enemy.get('round_hurt')>0:
		logging.info('runaway')
		gotoDie(dices, player, enemy)


def acidFog(dices, player, enemy):
	player['dice_cnt'] = 5
	gotoDie(dices, player, enemy)


def fireHurt(dices, player, enemy):
	cnt = 0
	for mem in player.get('team'):
		if mem.get('st')>0:
			mem['hp'] = mem.get('hp')-1
			cnt = cnt + 1
			if mem.get('hp')<=0:
				mem['hp'] = 0
				mem['st'] = 0
	player['weak'] = cnt			
	gotoDie(dices, player, enemy)


def changeDice(dices, player, enemy):
	player['dice_cnt'] = random.randint(1,6)


def cureSelfFac(pt):
	def cureSelf(dices, player, enemy):
		enemy['hp'] = enemy.get('hp') + pt
		if enemy.get('hp')>enemy.get('data').get('hp'):
			enemy['hp'] = enemy.get('data').get('hp')
	return cureSelf	


def getTeamHp(dices, player, enemy):
	hp = dices.count(5) + dices.count(6)
	enemy['hp'] = enemy.get('hp') + hp
	if enemy.get('hp')>enemy.get('data').get('hp'):
		enemy['hp'] = enemy.get('data').get('hp')


def hurtBoth(dices, player, enemy):
	hit = random.randint(1,6)
	player['enemy_hit'] = player.get('enemy_hit') + hit
	enemy['hp'] = enemy.get('hp') - hit


monsters = [
		{
			'id': 1000,
			'title': '测试01',
			'hp': 2,
			'hit': [6],
			'dice_fn': commonDice,
			},

		{
			'id': 1001,
			'title': '测试02',
			'hp': 7,
			'hit': [5,6],
			'dice_fn': commonDice,
			},

		{
			'id': 2000,
			'title': '兽人',
			'hp': 2,
			'hit': [5,6],
			'dice_fn': commonDice,
			},

		{
			'id': 2001,
			'title': '小妖',
			'hp': 1,
			'hit': [6],
			'dice_fn': commonDice,
			},

		{
			'id': 2002,
			'title': '大蜘蛛',
			'hp': 3,
			'hit': [5,6],
			'dice_fn': commonDice,
			'effect': '击晕',
			'eff_fn': exileDiceFac([1], '击晕'),
			},

		{
			'id': 2003,
			'title': '小蜘蛛',
			'hp': 2,
			'hit': [5,6],
			'dice_fn': commonDice,
			},

		{
				'id': 2004,
				'title': '巨人',
				'hp': 9,
				'hit': [4,5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 2005,
				'title': '骷髅兵',
				'hp': 2,
				'hit': [5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 2006,
				'title': '恶龙',
				'hp': 15,
				'hit': [5,6],
				'dice_fn': fixedDiceFac(6),
				},


		{
				'id': 3000,
				'title': '袭击者',
				'hp': 3,
				'hit': [5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 3001,
				'title': '蝎子',
				'hp': 1,
				'hit': [6],
				'dice_fn': commonDice,
				'effect': '呼唤同伴',
				'eff_fn': nextLevelFac(3001, 4),
				},

		{
				'id': 3002,
				'title': '掘地虫',
				'hp': 9,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '下逃',
				'eff_fn': nextLevelFac(3002, 1),
				},

		{
				'id': 3003,
				'title': '巨型蜈蚣',
				'hp': 4,
				'hit': [5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 3004,
				'title': '狮身人面兽',
				'hp': 3,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '攻击弱小',
				'eff_fn': attackWeakFac([5,6]),
				},

		{
				'id': 3005,
				'title': '时间陷阱',
				'hp': 12,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'effect': '时间终结',
				'eff_fn': killAllFac(3005,2),
				},

		{
				'id': 11,
				'title': '哥布林弓箭手',
				'hp': 2,
				'hit': [5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 17,
				'title': '树人',
				'hp': 8,
				'hit': [4,5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 24,
				'title': '刺藤',
				'hp': 3,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'effect': '伤害反弹',
				'eff_fn': hurtBack,
				},

		{
				'id': 20,
				'title': '妖精',
				'hp': 2,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'effect': '击晕',
				'eff_fn': exileDiceFac([6], '击晕'),
				},

		{
				'id': 33,
				'title': '黑暗巫妖王',
				'hp': 16,
				'hit': [5,6],
				'dice_fn': fixedDiceFac(6),
				'effect': '诅咒',
				'eff_fn': queueDice,
				},

		{
				'id': 38,
				'title': '陷阱:深渊',
				'hp': 1000,
				'hit': [6],
				'dice_fn': fixedDiceFac(18),
				'eff_fn': gotoDie,
				'trap': True,
				},

		{
				'id': 14,
				'title': '冰霜巨人',
				'hp': 4,
				'hit': [4,5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 18,
				'title': '食人魔',
				'hp': 4,
				'hit': [4,5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 21,
				'title': '雪狼',
				'hp': 4,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '冰冻伤口',
				'eff_fn': mustHurtFac(1),
				},

		{
				'id': 32,
				'title': '寒冰尸龙',
				'hp': 16,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '冰冻',
				'eff_fn': iceFrezz,
				},

		{
				'id': 41,
				'title': '陷阱:毒药',
				'hp': 1000,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'eff_fn': poison,
				'trap': True,
				},

		{
				'id': 12,
				'title': '屁民',
				'hp': 4,
				'hit': [6],
				'dice_fn': commonDice,
				'effect': '贪生怕死',
				'eff_fn': runaway,
				},

		{
				'id': 36,
				'title': '陷阱:酸雾',
				'hp': 1000,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'effect': '虚弱',
				'eff_fn': acidFog,
				'trap': True,
				},

		{
				'id': 19,
				'title': '暗影刺客',
				'hp': 4,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'effect': '天诛',
				'eff_fn': exileDiceFac([5,6], '天诛'),
				},

		{
				'id': 42,
				'title': '陷阱:灼热之焰',
				'hp': 1000,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'eff_fn': fireHurt,
				'trap': True,
				},

		{
				'id': 34,
				'title': '盗贼之王',
				'hp': 16,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '机关重重',
				'eff_fn': changeDice,
				},

		{
				'id': 13,
				'title': '灰矮人',
				'hp': 3,
				'hit': [5,6],
				'dice_fn': fixedDiceFac(1),
				},

		{
				'id': 28,
				'title': '巨魔',
				'hp': 3,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '自疗',
				'eff_fn': cureSelfFac(1),
				},

		{
				'id': 35,
				'title': '巴洛炎魔',
				'hp': 16,
				'hit': [],
				'dice_fn': fixedDiceFac(3),
				'effect': '秒杀',
				'eff_fn': exileDiceFac([6], '秒杀'),
				},

		{
				'id': 10,
				'title': '法师学徒',
				'hp': 3,
				'hit': [6],
				'dice_fn': commonDice,
				},

		{
				'id': 16,
				'title': '术士',
				'hp': 5,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '抽取生命',
				'eff_fn': getTeamHp,
				},

		{
				'id': 23,
				'title': '死灵法师军团',
				'hp': 16,
				'hit': [5,6],
				'dice_fn': commonDice,
				},

		{
				'id': 22,
				'title': '堕落神父',
				'hp': 18,
				'hit': [],
				'dice_fn': fixedDiceFac(1),
				'effect': '自爆',
				'eff_fn': hurtBoth,
				},

		{
				'id': 25,
				'title': '超能者',
				'hp': 3,
				'hit': [5,6],
				'dice_fn': commonDice,
				'effect': '攻击弱小',
				'eff_fn': attackWeakFac([5,6]),
				},

		]


monstermap = dict()
for monster in monsters:
	monstermap[monster.get('id')] = monster


hurt_option = [{'id': 0, 'title': ''}]
for i in range(1,13):
	hurt_option.append({'id': i, 'title': '扣血%d' % i})
hurt_option.append({'id': 'kill', 'title': '即死'})	


cure_option = [{'id': 0, 'title': ''}]
for i in range(1,7):
	cure_option.append({'id': i, 'title': '加血/扣血%d' % i})
cure_option.append({'id': 'resurrection', 'title': '复活'})	
cure_option.append({'id': 'gandr', 'title': '献祭'})	
cure_option.append({'id': 'exile', 'title': '击晕'})
cure_option.append({'id': 'exile', 'title': '天诛'})
cure_option.append({'id': 'exile', 'title': '秒杀'})
